<?php
$mycs = Yii::app()->getClientScript();
$urlScript_ckeditor = bu() . '/js/ckeditor/ckeditor.js';
$mycs->registerScriptFile($urlScript_ckeditor, CClientScript::POS_HEAD);
if (!$model->isNewRecord) {
    //truong hop update record
    $listMeta = ObjectMeta::model()->findAll('meta_object_id = :obj && meta_key!=:key1 && meta_key!=:key2', array(':obj' => $model->object_id,
        ':key1' => 'sku',
        ':key2' => 'price_USD'));
    $list = array();
    $lCurrency = array();
    $formAddCurrency = '';
    if ($listMeta) {
        foreach ($listMeta as $lm) {
            $meta_key = explode('_', $lm['meta_key']);
            $list[] = $meta_key[1];
            $formAddCurrency .= '<div class="tab-content default-tab" id="extra_box_' . $meta_key[1] . '">
                                    <label class="required" for="ProductObject_price">Price [' . $meta_key[1] . ']</label>
                                    <input type="text" value="' . $lm['meta_value'] . '" name="priceCurrency[' . $meta_key[1] . ']" style="width: 200px;">&nbsp;&nbsp;' .
                    CHtml::button("[ X ] Remove", array('class' => 'bebutton active', 'title' => "Remove currency", 'onclick' => 'js:deleteCurrency("' . $meta_key[1] . '", ' . $model->object_id . ');'))
                    . '</div>';
        }
        $result = str_replace("[", "(", json_encode($list));
        $result = str_replace("]", ")", $result);
        $lCurrency = Currency::model()->findAll('codeTo not in ' . $result, array('order' => 'codeTo ASC'));
    } else {
        $lCurrency = Currency::model()->findAll(array('order' => 'codeTo ASC'));
    }
} else {
    //truong hop tao moi record
    $lCurrency = Currency::model()->findAll(array('order' => 'codeTo ASC'));
}
$data = array();
if ($lCurrency && count($lCurrency) > 0) {
    $data = CHtml::listData($lCurrency, 'codeTo', 'codeTo');
}
?>

<div class="form">
    <?php $this->render('cmswidgets.views.notification'); ?>
    <?php
    $form = $this->beginWidget('CActiveForm', array(
        'id' => 'object-form',
        'enableAjaxValidation' => false,
    ));
    ?>
    <?php echo $form->errorSummary($model); ?>
    <div class="form-wrapper">
        <div id="form-sidebar">
            <?php $this->render('cmswidgets.views.object.object_publish_sidebar_form', array('form' => $form, 'model' => $model, 'content_status' => $content_status, 'type' => $type, 'terms' => $terms, 'selected_terms' => $selected_terms)); ?>
        </div>
        <div id="form-body">
            <div id="form-body-content">

                <!-- //Render Partial for Object Language Zone & Name & Content-->
                <?php $this->render('cmswidgets.views.object.object_language_name_content_widget', array('model' => $model, 'type' => $type, 'form' => $form, 'versions' => $versions, 'lang_exclude' => $lang_exclude)); ?>
                <div class="row">
                    <!-- //Render Partial for Resource Binding -->
                    <?php $this->render('cmswidgets.views.object.object_resource_form_widget', array('model' => $model, 'type' => $type, 'content_resources' => $content_resources)); ?>                    	
                </div>

                <div class="row">
                    <!--Start the Meta Box -->


                    <div id="vinhtest"></div>
                    <div class="content-box">
                        <div class="content-box-header">
                            <h3><?php echo t('site', 'Content Product Sku'); ?></h3>                             
                        </div> 
                        <div class="content-box-content" style="display: block;">
                            <div class="tab-content default-tab" id="extra_box">
                                <?php echo $form->labelEx($model, 'sku'); ?>
                                <?php echo $form->textField($model, 'sku'); ?>
                                <?php echo $form->error($model, 'sku'); ?>
                            </div>
                        </div>
                    </div>
                    <div class="content-box">
                        <div class="content-box-header">
                            <h3><?php echo t('site', 'Content Product Currency'); ?></h3>                             
                        </div> 

                        <div class="content-box-content" style="display: block;">
                            <div class="tab-content default-tab" id="extra_box">
                                <?php echo $form->labelEx($model, 'price_USD'); ?>
                                <?php echo $form->textField($model, 'price_USD'); ?>
                                <?php echo $form->error($model, 'price_USD'); ?>
                                <div id="add-currency"></div>
                            </div> 
                            <?php
                            //vinh note:
                            //phan add va remove currency not working when update object
                            if (!$model->isNewRecord) {
                                echo $formAddCurrency;
                            }
                            ?>
                            <div id="vinhtest"></div>
                            <div class="select_country">
                                <div id="ajaxLoading" style="display: none;"></div>
                                <div>  
                                    <?php
                                    echo Chtml::dropDownList('currency', '', $data, array());
                                    echo '&nbsp;&nbsp;';
                                    echo CHtml::ajaxSubmitButton(
                                            'Add', array('ajax/addCurrency'), array(
                                        //'update'=>'#vinhtest',

                                        'success' => 'function(data){  
                                                            var output = jQuery.parseJSON(data);
                                                            form = output.form;
                                                            curr = output.currency;
                                                            $("#add-currency").append(form);
                                                            $("select#currency option[value=\'" + curr + "\']").remove();
                                                            $("#ajaxLoading").hide();
                                                        }',
                                        'beforeSend' => 'function(){                        
                                                            $("#ajaxLoading").show();
                                                        }',
                                            ), array('class' => 'bebutton active',));
                                    ?>
                                </div>
                            </div>
                        </div>

                    </div>
                    <!-- End the Meta Box -->  

                    <!--Start the Summary and SEO Box -->
                    <div class="content-box ">
                        <!-- //Render Partial for SEO -->
                        <?php $this->render('cmswidgets.views.object.object_seo_form_widget', array('model' => $model, 'form' => $form)); ?>
                    </div>
                    <!-- End Summary and SEO Box -->                            
                </div>

            </div>
        </div>

    </div>
    <br class="clear" />
    <?php $this->endWidget(); ?>
</div><!-- form -->

<!-- //Render Partial for Javascript Stuff -->
<?php $this->render('cmswidgets.views.object.object_form_javascript', array('model' => $model, 'form' => $form)); ?>
<script>
    function deleteCurrency(curr) {
        //alert('vinh show '+curr);
        $('#extra_box_' + curr).remove();
        $('select#currency').prepend('<option value="' + curr + '">' + curr + '</option>');
        $("select#currency").selectOptionSort();
    }
</script>